var modalLock = false

interface ModalConfig {
    title?: string;
    message: string;
    showCancel?: boolean;
    cancelText?: string;
    confirmText?: string;
    confirmColor?: string;
}

interface ModalResolve {
    confirm: boolean
    cancel: boolean
}

export default {
    toast(msg : string ) {
        msg = msg + ''
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                uni.showToast({
                    icon: 'none',
                    title: msg,
                    duration: 3000,
                    success: resolve,
                    fail: reject
                });
            })
        });
    },
    modal(msg: string | ModalConfig) {
        return new Promise<ModalResolve>((resolve, reject) => {
            if(modalLock) return reject()
            modalLock = true
            let params;
            if(typeof msg === 'string') {
                params = {
                    content: msg,
                    showCancel: false
                };
            } else {
                params = {
                    showCancel: false,
                    ...msg
                }
            }
            uni.showModal(Object.assign({
                title: params.title || '',
                content: params.message,
                showCancel: params.showCancel,
                success: resolve,
                fail: reject,
                complete(){
                    modalLock = false
                }
            }, params));
        });
    }
}
